
//나이 데이터 수집용.
var ages = { a0: 0, a10: 0, a20: 0, a30: 0, a40: 0, a50: 0, a60: 0, a70: 0 };
var areas = new Array();
var jobs = new Array();
var areaIndex = 0;
var jobIndex = 0;

function init() {
	
	var json = JSON.parse( $("#json").html() );
	
	for ( var i = 0; i < json.loginList.length; i++ ) {
		//전체.
		if ( json.loginList[i].t_type == "all" ) {
			var temp1 = json.loginList[i].day.split(" ");
            var temp2 = temp1[0].split("-");
            var day = temp2[2];
            
            day = parseInt( day );
            $("#yearAndMonth").html( "< " + temp2[0] + "년 " + temp2[1] + "월 >" );
            $("#"+day).html( json.loginList[i].sum );
        
		} else if ( json.loginList[i].t_type == "marry" ) {
			//결혼유무.
			if( json.loginList[i].t_value == "true" ) {
				var beforeValue = $("#marry_true").html() == "" ? 0 : $("#marry_true").html();
				$("#marry_true").html( parseInt( beforeValue ) + parseInt( json.loginList[i].sum));
			} else {
				var beforeValue = $("#marry_false").html() == "" ? 0 : $("#marry_false").html();
				$("#marry_false").html( parseInt( beforeValue ) + parseInt( json.loginList[i].sum));
				
			}
		} else if ( json.loginList[i].t_type == "age" ) {
			//나이대.
			var beforeValue = $(ages).attr("a"+json.loginList[i].t_value) == "" ? 0 : $(ages).attr("a"+json.loginList[i].t_value);
			$(ages).attr("a"+json.loginList[i].t_value, parseInt( beforeValue ) + parseInt( json.loginList[i].sum));
			
		} else if ( json.loginList[i].t_type == "area" ) {
			//지역별.
			var obj = new Object();
			obj['area'] = json.loginList[i].t_value;
			obj['count'] = json.loginList[i].sum;
			areas[areaIndex++] = obj;
			
		} else if ( json.loginList[i].t_type == "job" ) {
			//직업별.
			var obj = new Object();
			obj['job'] = json.loginList[i].t_value;
			obj['count'] = json.loginList[i].sum;
			jobs[jobIndex++] = obj;
			
		} else if ( json.loginList[i].t_type == "sex" ) {
			//성별.
			if ( json.loginList[i].t_value == "man" ) {
				var beforeValue = $("#sex_man").html() == "" ? 0 : $("#sex_man").html();
				$("#sex_man").html( parseInt( beforeValue ) + parseInt( json.loginList[i].sum ));
			} else {
				var beforeValue = $("#sex_woman").html() == "" ? 0 : $("#sex_woman").html();
				$("#sex_woman").html( parseInt( beforeValue ) + parseInt( json.loginList[i].sum ));
			}
			
		}
	}
	//나이대 내림차순 정렬.
	var ageArray = new Array(8);
	for ( var i = 0; i < ageArray.length; i++ ) {
		ageArray[i] = {
			"age": "a"+(i*10),
			"count": $(ages).attr("a"+(i*10))
		};
	}
	ageArray = sort( ageArray, "age" );
	for ( var i = 0; i < ageArray.length; i++ ) {
		$("#age"+(i+1)).html( ageArray[i].age.substring(1) + "대" );
		$("#ageCount"+(i+1)).html( ageArray[i].count );
	}
	
	//지역별 내림차순 정렬.
	areas = sort( areas, "area" );
	for ( var i = 0; i < areas.length; i++ ) {
		$("#area"+(i+1)).html( areas[i].area );
		$("#areaCount"+(i+1)).html( areas[i].count );
	}
	
	//직업별 내림차순 정렬.
	jobs = sort( jobs, "job" );
	for ( var i = 0; i < jobs.length; i++ ) {
		$("#job"+(i+1)).html( jobs[i].job );
		$("#jobCount"+(i+1)).html( jobs[i].count );
	}
	
	//합계.
	for( var i = 1; i <= 31; i++ ) {
		var sum = $("#sum").html();
		if ( sum == "" ) {
			sum = 0;
		}
		if ( $("#"+i).html() != "" ) {
			sum = parseInt( sum ) + parseInt( $("#"+i).html() );
		}
		$("#sum").html( sum );
	}
}

function init2() {
	
	var json = JSON.parse( $("#json").html() );
	
	for ( var i = 0; i < json.loginList.length; i++ ) {
		//전체.
		if ( json.loginList[i].t_type == "all" ) {
			var temp1 = json.loginList[i].day.split(" ");
            var temp2 = temp1[0].split("-");
            
            $("#yearAndMonth").html( "< " + temp2[0] + "년 " + temp2[1] + "월 " + temp2[2] + "일 >" );
            for( var j = 0; j < 24; j++ ) {
            	$("#" + j).html( $(json.loginList[i]).attr("h"+j) );
            }
        
		} else if ( json.loginList[i].t_type == "marry" ) {
			//결혼유무.
			if( json.loginList[i].t_value == "true" ) {
				var beforeValue = $("#marry_true").html() == "" ? 0 : $("#marry_true").html();
				$("#marry_true").html( parseInt( beforeValue ) + parseInt( json.loginList[i].sum));
			} else {
				var beforeValue = $("#marry_false").html() == "" ? 0 : $("#marry_false").html();
				$("#marry_false").html( parseInt( beforeValue ) + parseInt( json.loginList[i].sum));
				
			}
		} else if ( json.loginList[i].t_type == "age" ) {
			//나이대.
			var beforeValue = $(ages).attr("a"+json.loginList[i].t_value) == "" ? 0 : $(ages).attr("a"+json.loginList[i].t_value);
			$(ages).attr("a"+json.loginList[i].t_value, parseInt( beforeValue ) + parseInt( json.loginList[i].sum));
			
		} else if ( json.loginList[i].t_type == "area" ) {
			//지역별.
			var obj = new Object();
			obj['area'] = json.loginList[i].t_value;
			obj['count'] = json.loginList[i].sum;
			areas[areaIndex++] = obj;
			
		} else if ( json.loginList[i].t_type == "job" ) {
			//직업별.
			var obj = new Object();
			obj['job'] = json.loginList[i].t_value;
			obj['count'] = json.loginList[i].sum;
			jobs[jobIndex++] = obj;
			
		} else if ( json.loginList[i].t_type == "sex" ) {
			//성별.
			if ( json.loginList[i].t_value == "man" ) {
				var beforeValue = $("#sex_man").html() == "" ? 0 : $("#sex_man").html();
				$("#sex_man").html( parseInt( beforeValue ) + parseInt( json.loginList[i].sum ));
			} else {
				var beforeValue = $("#sex_woman").html() == "" ? 0 : $("#sex_woman").html();
				$("#sex_woman").html( parseInt( beforeValue ) + parseInt( json.loginList[i].sum ));
			}
			
		}
	}
	//나이대 내림차순 정렬.
	var ageArray = new Array(8);
	for ( var i = 0; i < ageArray.length; i++ ) {
		ageArray[i] = {
			"age": "a"+(i*10),
			"count": $(ages).attr("a"+(i*10))
		};
	}
	ageArray = sort( ageArray, "age" );
	for ( var i = 0; i < ageArray.length; i++ ) {
		$("#age"+(i+1)).html( ageArray[i].age.substring(1) + "대" );
		$("#ageCount"+(i+1)).html( ageArray[i].count );
	}
	
	//지역별 내림차순 정렬.
	areas = sort( areas, "area" );
	for ( var i = 0; i < areas.length; i++ ) {
		$("#area"+(i+1)).html( areas[i].area );
		$("#areaCount"+(i+1)).html( areas[i].count );
	}
	
	//직업별 내림차순 정렬.
	jobs = sort( jobs, "job" );
	for ( var i = 0; i < jobs.length; i++ ) {
		$("#job"+(i+1)).html( jobs[i].job );
		$("#jobCount"+(i+1)).html( jobs[i].count );
	}
	
	//합계.
	for( var i = 0; i < 24; i++ ) {
		var sum = $("#sum").html();
		if ( sum == "" ) {
			sum = 0;
		}
		if ( $("#"+i).html() != "" ) {
			sum = parseInt( sum ) + parseInt( $("#"+i).html() );
		}
		$("#sum").html( sum );
	}
}


//내림차순 정렬.
function sort( array, key ) {
	var sortedArray = new Array();
	var sortedArrayCount = 0;
	
	for ( var i = 0; i < array.length - 1; i++ ) {
		for ( var j = i+1; j < array.length; j++ ) {
			if ( $(array[i]).attr(key) == $(array[j]).attr(key) ) {
				array[i].count = parseInt( array[i].count ) + parseInt( array[j].count );
				array[j].count = 0;
			}
		}
	}
	
	for ( var i = 0; i < array.length; i++ ) {
		if ( array[i].count != 0 ) {
			sortedArray[sortedArrayCount++] = array[i];
		}
	}
	
	for ( var i = 0; i < sortedArray.length - 1; i++ ) {
		for ( var j = i+1; j < sortedArray.length; j++ ) {
			if ( sortedArray[i].count < sortedArray[j].count ) {
				var temp = sortedArray[i];
				sortedArray[i] = sortedArray[j];
				sortedArray[j] = temp;
			}
		}
	}
	return sortedArray;
}













